package tests
{
	import com.as3undohistory.register.IAccessorRegistration;
	import com.as3undohistory.register.IObjectRegistration;
	import com.as3undohistory.register.IUndoClassRegistration;
	import com.as3undohistory.register.ObjectRegistration;
	
	import flexunit.framework.TestCase;
	
	import tests.testclasses.UndoAbleTestClass;
	import tests.testclasses.UndoableTestClassWithoutBinding;
	import tests.testclasses.UndoableTestClassWithoutEventKey;

	public class ObjectRegistrationTest extends TestCase
	{
		public function ObjectRegistrationTest(methodName:String=null)
		{
			super(methodName);
		}
		
		public function testRegisterClass():void
		{
			var objReg:IObjectRegistration = new ObjectRegistration();
			var undoreg:IUndoClassRegistration = objReg.registerClass(UndoAbleTestClass);
			assertNotNull(undoreg);
			assertEquals(UndoAbleTestClass,undoreg.clazz);
			assertEquals(2,undoreg.accessorRegistrations.length);
		}
		
		public function testRegisterClassWithExtraNamesList():void
		{
			var objReg:IObjectRegistration = new ObjectRegistration();
			var undoreg:IUndoClassRegistration = objReg.registerClass(UndoAbleTestClass,["testDateProperty"]);
			assertNotNull(undoreg);
			assertEquals(UndoAbleTestClass,undoreg.clazz);
			assertEquals(3,undoreg.accessorRegistrations.length);
		}

		public function testRegisterClassWithExtraNamesListAndDoubleAccessorNames():void
		{
			var objReg:IObjectRegistration = new ObjectRegistration();
			var undoreg:IUndoClassRegistration = objReg.registerClass(UndoAbleTestClass,["testDateProperty","testStringProperty"]);
			assertNotNull(undoreg);
			assertEquals(UndoAbleTestClass,undoreg.clazz);
			assertEquals(3,undoreg.accessorRegistrations.length);
		}

		public function testRegisterClassWithNotBindableError():void
		{
			var objReg:IObjectRegistration = new ObjectRegistration();
			try
			{
				var undoreg:IUndoClassRegistration = objReg.registerClass(UndoableTestClassWithoutBinding);
			}
			catch(e:*)
			{
				assertEquals(e.message,"Property testStringProperty is not bindable");
			}
		}

		public function testRegisterClassWithMissingEventKeyError():void
		{
			var objReg:IObjectRegistration = new ObjectRegistration();
			try
			{
				var undoreg:IUndoClassRegistration = objReg.registerClass(UndoableTestClassWithoutEventKey);
			}
			catch(e:*)
			{
				assertEquals(e.message,"Property testStringProperty's Bindable metadata does not have an event key");
			}
		}
		
		public function testIsClassRegistered():void
		{
			var objReg:IObjectRegistration = new ObjectRegistration();
			var undoreg:IUndoClassRegistration = objReg.registerClass(UndoAbleTestClass);
			assertTrue(true,objReg.isClassRegistered(UndoAbleTestClass));
		}

	}
}